<?php
class PowerAction extends BaseAction{
	public function addPower(){
		$this->assign("position" ,"权限添加");
		if ( $this->isPost() ) {
			if ( $_POST['power'] ) {
				foreach ( $_POST['power'] as $key=>$value ) {
					$data['admin_id'] = $_POST['name'];
					$data['power_id'] = $value;
					if ( !M("adminpower")->where($data)->find()){
						$result[] = M("adminpower")->data($data)->add();
					}
				}
				
				if ( $result ) {
					$this->assign("jumpUrl","__URL__/addPower");
					$this->assign("waitSecond",3);
					$this->success("权限添加成功！！！");die;
				} else {
					$this->assign("jumpUrl","__URL__/addPower");
					$this->assign("waitSecond",3);
					$this->error("权限添加失败！！！");
				}
			} else {
				$this->assign("jumpUrl","__URL__/addPower");
				$this->assign("waitSecond",3);
				$this->error("请选择权限！！！");
			}
		}
		$allAdminPower = M("power")->order("power_order asc")->select();
		import('@.ORG.Power');
		$power = new Power();
		$module = $power->index();
		foreach ( $allAdminPower as $kye=>$value ) {
			$allModule = explode("/", $value['power_action']);
			foreach ( $module as $k=>$v) {
				if ( $k == $allModule['0'] ){
					$powers[$v][] = $value;
				}
			}
		}
		$allAdmin = M("admin")->field("id,admin_name")->where("admin_name != 'letslegend' and admin_name != 'vinnie' and admin_name !='".$_SESSION['administer']['name']."'")->select();
		$this->assign("allpower" ,$powers);
		$this->assign("alladmin" ,$allAdmin);
		$this->display();
	}
	
	public function checkPower(){
		$this->assign("position" ,"权限查看");
		if ( $this->isPost() ) {
			$allAdminPower = M("adminpower")->field("power.power_name,power.power_id,power.power_action")->join("admin ON admin.id = adminpower.admin_id")->join("power ON power.power_id = adminpower.power_id")->where("adminpower.admin_id=".$_POST['id'])->select();

			import('@.ORG.Power');
			$power = new Power();
			$module = $power->index();
			foreach ( $allAdminPower as $kye=>$value ) {
				$allModule = explode("/", $value['power_action']);
				foreach ( $module as $k=>$v) {
					if ( $k == $allModule['0'] ){
						$powers[$v][] = $value;
					}
				}
			}
			$this->assign("adminpower" , $powers);
			$this->display("showPower");
			die;
		}
		
		$allAdmin = M("admin")->field("id,admin_name")->where("admin_name != 'letslegend' and admin_name != 'vinnie' and admin_name !='".$_SESSION['administer']['admin_name']."'")->select();
		
		$this->assign("alladmin" ,$allAdmin);
		$this->display();
	}
	
	public function delPower(){
		$this->assign("position" ,"权限删除");
		if ( $_GET ) {
			$allAdminPower = M("adminpower")->field("power.power_name,power.power_id,power.power_action")->join("admin ON admin.id = adminpower.admin_id")->join("power ON power.power_id = adminpower.power_id")->where("adminpower.admin_id=".$_GET['id'])->select();

			import('@.ORG.Power');
			$power = new Power();
			$module = $power->index();
			foreach ( $allAdminPower as $kye=>$value ) {
				$allModule = explode("/", $value['power_action']);
				foreach ( $module as $k=>$v) {
					if ( $k == $allModule['0'] ){
						$powers[$v][] = $value;
					}
				}
			}
			$this->assign("adminpower" , $powers);
			$this->display("deletePower");
			die;
		}
		
		if ($this->isPost()){
			foreach ( $_POST['power'] as $key=>$value ) {
				$data['admin_id'] = $_POST['name'];
				$data['power_id'] = $value;
				if ( M("adminpower")->where($data)->find()){
					$result[] = M("adminpower")->where($data)->delete();
				}
			}
			//var_dump($result);die;
			if ( $result ) {
				$this->assign("jumpUrl","__URL__/delPower");
				$this->assign("waitSecond",3);
				$this->success("权限删除成功！！！");die;
			} else {
				$this->assign("jumpUrl","__URL__/delPower");
				$this->assign("waitSecond",3);
				$this->error("权限删除失败！！！");
			}
		}
		$allAdmin = M("admin")->field("id,admin_name")->where("admin_name != 'letslegend' and admin_name != 'vinnie' and admin_name !='".$_SESSION['administer']['name']."'")->select();
// 		var_dump($_SESSION['administer']);
// 		echo M("admin")->getLastSql();die;
		$this->assign("alladmin" ,$allAdmin);
		$this->display();
	}
	
}